﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using OnlineVideos.Data;

using Common.Library;
using System.Windows.Media.Imaging;
using OnlineVideos.UI;
using OnlineVideos.Entities;
using System.Threading.Tasks;

namespace OnlineVideos
{
    public partial class DownloadedImagePopup : PhoneApplicationPage
    {
        #region GlobalDeclaration
        public static int pat;
        public static int filecount;
        public static int fileid = 1;
        public static int ind1;
        public static string path1;
        public static List<string> filenames;
        double x;
        public string galleryImageCount;
        #endregion
        public DownloadedImagePopup()
        {
            InitializeComponent();
            Application.Current.Host.Content.Resized += (s, e) =>
            {
                LayoutRoot.Width = Application.Current.Host.Content.ActualWidth;
                LayoutRoot.Height = Application.Current.Host.Content.ActualHeight;
            };

            filenames = new List<string>();
            galleryImageCount = string.Empty;

            Loaded += new RoutedEventHandler(DownloadedImagePopup_Loaded);
        }

        void DownloadedImagePopup_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                string img = "";
                if (AppSettings.LinkId == string.Empty)
                {
                    int showid = Convert.ToInt32(AppSettings.ShowID);
                    List<ShowList> selectedRow = Task.Run(async()=> await Constants.connection.Table<ShowList>().Where(i=>i.ShowID==showid).ToListAsync()).Result;
                    foreach (var item in selectedRow)
                    {
                        img = item.Title;
                        tblkVideosTitle.Text = System.IO.Path.GetFileNameWithoutExtension(img);
                    }
                }
                else
                {
                    int showid=Convert.ToInt32(AppSettings.ShowID);
                    int linkid=Convert.ToInt32(AppSettings.LinkId);
                    List<ShowLinks> selectedRow = Task.Run(async () => await Constants.connection.Table<ShowLinks>().Where(i => i.ShowID == showid && i.LinkID == linkid).ToListAsync()).Result;
                    foreach (var item in selectedRow)
                    {
                        img = item.Title;
                        tblkVideosTitle.Text = System.IO.Path.GetFileNameWithoutExtension(img);
                    }
                }
                galleryImageCount = AppSettings.gallcount;
                AppSettings.bcount = true;
                ind1 = img.IndexOf('.');
                path1 = img;
                SetImageSource(path1);
            }
            catch (Exception ex)
            {
                string excepmess = "Exception in DownloadedImagePopup_Loaded Method In downloadimagepopup file.\n\n" + ex.Message + "\n\n StackTrace :- \n" + ex.StackTrace;
                Exceptions.SaveExceptionInLocalStorage(excepmess);
            }
        }

        public void SetImageSource(string imagePath)
        {
            try
            {
                BitmapImage bmImage = Storage.ReadBitmapImageFromFile(ResourceHelper.ProjectName+"/Images/"+imagePath, BitmapCreateOptions.DelayCreation | BitmapCreateOptions.BackgroundCreation);
                image1.Stretch = Stretch.Uniform;
                image1.MaxHeight = 800;
                image1.MaxHeight = 480;
                image1.Source = bmImage;

                this.Visibility = System.Windows.Visibility.Visible;
            }
            catch (Exception ex)
            {
                string excepmess = "Exception in DownloadedImagePopup_Loaded Method In downloadimagepopup file.\n\n" + ex.Message + "\n\n StackTrace :- \n" + ex.StackTrace;
                Exceptions.SaveExceptionInLocalStorage(excepmess);
            }
        }
        void style()
        {
            try
            {
                image1.RenderTransform = new ScaleTransform();

                Duration duration = new Duration(TimeSpan.FromSeconds(0));

                DoubleAnimation myDoubleAnimation1 = new DoubleAnimation();

                myDoubleAnimation1.Duration = duration;
                Storyboard sb = new Storyboard();
                sb.Duration = duration;
                sb.Children.Add(myDoubleAnimation1);
                Storyboard.SetTarget(myDoubleAnimation1, image1);
                Storyboard.SetTargetProperty(myDoubleAnimation1, new PropertyPath("Opacity"));
                myDoubleAnimation1.To = 10.00;
                myDoubleAnimation1.From = 0.00;

                LayoutRoot.Resources.Add("unique_id", sb);

                sb.Begin();

                LayoutRoot.Resources.Remove("unique_id");
            }
            catch (Exception ex)
            {
                string excepmess = "Exception in DownloadedImagePopup_Loaded Method In downloadimagepopup file.\n\n" + ex.Message + "\n\n StackTrace :- \n" + ex.StackTrace;
                Exceptions.SaveExceptionInLocalStorage(excepmess);
            }
        }

        #region Events
        private void image1_ManipulationCompleted_1(object sender, ManipulationCompletedEventArgs e)
        {
            try
            {
                var gallery = Task.Run(async () => await Constants.connection.Table<ShowLinks>().ToListAsync()).Result;
                foreach (var gal in gallery)
                {
                    filenames.Add(gal.LinkUrl);
                }
                if (x < 0)
                {
                    if (filecount == int.Parse(galleryImageCount) - 1)
                    {
                        fileid = 0;
                        SetImageSource(filenames[fileid].ToString());
                        filecount = (fileid + 1);
                        fileid++;
                    }
                    else
                    {
                        filecount = Convert.ToInt32(fileid) + 1;
                        SetImageSource(filenames[fileid].ToString());
                        filecount = fileid;
                        fileid++;
                    }
                }
                if (x > 0)
                {
                    if (filecount.ToString() == "0")
                    {
                        fileid = int.Parse(galleryImageCount);
                        SetImageSource(filenames[fileid].ToString());
                        filecount = (fileid - 1);
                        fileid--;
                    }
                    else
                    {
                        filecount = Convert.ToInt32(fileid) - 1;
                        SetImageSource(filenames[fileid].ToString());
                        filecount = (fileid);
                        fileid--;
                    }
                }


            }
            catch (Exception ex)
            {

                Exceptions.SaveOrSendExceptions("Exception in image1_ManipulationCompleted_1 Method In persongallerypopup.xaml.cs file", ex);
            }
        }
        private void image1_ManipulationDelta(object sender, ManipulationDeltaEventArgs e)
        {
            x = e.DeltaManipulation.Translation.X;
        }
        #endregion

        private void imgTitle_MouseEnter(object sender, MouseButtonEventArgs e)
        {
            NavigationService.Navigate(NavigationHelper.MainPanoramaPage);
        }
    }
}